package tool.workflow.fsSoft

import dao._
import implicits.Implicits._
import play.api.i18n.Messages
import tool.Pojo._
import shared.Pojo._
import shared.pojo.WorkflowMission.EachCorData
import shared.tool.DefaultPickle
import tool._
import tool.plot._
import tool.softT._
import tool.workflow.fsSoft.AllFsSoft.FSSoftBT

import java.io.File
import scala.collection.immutable.SeqMap

/** Created by yz on 3/12/2020
  */
object FSCor extends FSSoftBT with CorSoftBT {

  override def getFileInfos(workspaceDir: File, index: Int)(implicit
      matrixFileDataDao: MatrixFileDataDao,
      messages: Messages
  ): List[FileInfoData] = {
    List(
      FileInfoData("Correlation_Heatmap.pdf", messages("correlationHeatmap")),
      FileInfoData("r_Matrix.csv", messages("rMatrix")),
      FileInfoData("P_Matrix.csv", messages("pMatrix")),
      FileInfoData("Corrected_P_Matrix.csv", messages("correctedPMatrix")),
      FileInfoData("Network_Nodes_for_Cytoscape.csv", messages("networkNodesForCytoscape")),
      FileInfoData("Network_Edges_for_Cytoscape.csv", messages("networkEdgesForCytoscape")),
      FileInfoData("Diff_Metabolite.csv", messages("diffMetabolite"))
    )
  }

  def produceSubConfigFile(workspaceDir: File, data: EachCorData) = {
    val subConfigFile = new File(workspaceDir, "calculate_config.json")
    DefaultPickle.write(data).toFile(subConfigFile)
  }

  override def produceInput(workspaceDir: File, resultDir: File, index: Int, parentNodeStr: String, id: String)(implicit
      dao: MyDao,
      messages: Messages
  ): Unit = {
    implicit val missionDao = dao.missionDao
    val groupFile = new File(workspaceDir.getParentFile, "group.txt")
    groupFile.fileCopyToDir(workspaceDir)
    FeatureSelectionTool.produceSubInputFile(workspaceDir, parentNodeStr)
    val data = FeatureSelectionTool.getFsData(workspaceDir.getParentFile).cor(index)
    produceSubConfigFile(workspaceDir, data)
    val extraDataFile = new File(workspaceDir, "Extra_AllMet_Raw.txt")
    val userIdR = "/user/(\\d+)/.*$".r
    val userId = userIdR
      .findFirstMatchIn(workspaceDir.unixPath)
      .map { matcher =>
        matcher.group(1)
      }
      .get
      .toInt
    Tool.produceRawMetaboliteFile(userId, data.extraFile, extraDataFile)
    if (data.hasConfounder) {
      val confounderFile = new File(workspaceDir, "confounder_data.txt")
      Tool.produceRawMetaboliteFile(userId, data.confounderFile, confounderFile)
    }
  }

  override def getCommandData(workspaceDir: File, resultDir: File, index: Int): List[CommandData] = {
    val solutionFile = new File(Tool.dataDir, "color_solution/color_solution_1.txt")
    val rawDataFile = new File(workspaceDir, "AllMet_Raw.txt")
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Tool.rPath, "fs_cor_calculate.R").unixPath}
         |${"Rscript".condaPath()} ${new File(Tool.rPath, "cor_filter.R").unixPath}
           """.stripMargin
    val plotCommand = plot.CorHeatmapPlot.getCommand(workspaceDir, "")
    List(CommandData(workspaceDir, List(command, plotCommand)))
  }

  override def producePlotConfigFile(workspaceDir: File, index: Int): Any = {
    plot.CorHeatmapPlot.initConfigFile(workspaceDir, idStr = "")
  }

}
